{template '_header'}

<style type='text/css' xmlns="http://www.w3.org/1999/html">
    .dd-handle { height: 40px; line-height: 30px}
    .dd-list { width:860px;}
</style>


<div class="page-heading"> 
    <span class="pull-right">
        {ifp 'sysset.category'}
        <a href="{php echo webUrl('sysset/category')}" class="btn btn-warning"> 设置分类层级</a>
        {/if}
        <button type="button" id='btnExpand' class="btn btn-default" data-action='expand'><i class='fa fa-angle-up'></i> 折叠所有</button>  
        {ifp 'goods.category.add'}
        <a href="{php echo webUrl('goods/category/add')}" class="btn btn-primary"><i class="fa fa-plus"></i> 添加新分类</a>
        {/if}

    </span>
    <h2><i class="fa fa-circle-o"></i>商品分类</h2>
</div>
<form action="" method="post" class="form-validate">

    <div class="dd" id="div_nestable">
        <ol class="dd-list">

            {loop $category $row}
            {if empty($row['parentid'])}
            <li class="dd-item full" data-id="{$row['id']}">

                <div class="dd-handle" >
                    [ID: {$row['id']}] {$row['name']}
                    <span class="pull-right">

                        <div class='label {if $row['enabled']==1}label-success{else}label-default{/if}'
                             {ifp 'goods.category.edit'}
                             data-toggle='ajaxSwitch'
                             data-switch-value='{$row['enabled']}'
                             data-switch-value0='0|隐藏|label label-default|{php echo webUrl('goods/category/enabled',array('enabled'=>1,'id'=>$row['id']))}'
                             data-switch-value1='1|显示|label label-success|{php echo webUrl('goods/category/enabled',array('enabled'=>0,'id'=>$row['id']))}'
                             {/if}
                             >
                             {if $row['enabled']==1}显示{else}隐藏{/if}</div>

                               {if intval($_W['shopset']['category']['level'])>1 }{ifp 'goods.category.add'}<a class='btn btn-default btn-sm' href="{php echo webUrl('goods/category/add', array('parentid' => $row['id']))}" title='添加子分类' ><i class="fa fa-plus"></i></a>{/if}{/if}
                               {ifp 'goods.category.edit|goods.category.view'}
                               <a class='btn btn-default btn-sm' href="{php echo webUrl('goods/category/edit', array('id' => $row['id']))}" title="{ifp 'goods.category.edit'}修改{else}查看{/if}" ><i class="fa fa-edit"></i></a>
                               {/if}
                               {ifp 'goods.category.delete'}<a class='btn btn-default btn-sm' data-toggle='ajaxPost' href="{php echo webUrl('goods/category/delete', array('id' => $row['id']))}" data-confirm='确认删除此分类吗？'><i class="fa fa-remove"></i></a>{/if}
                        </span>
                    </div>
                    {if count($children[$row['id']])>0}

                    <ol class="dd-list">
                        {loop $children[$row['id']] $child}
                        <li class="dd-item full" data-id="{$child['id']}">
                            <div class="dd-handle" style="width:100%;">
                                <img src="{php echo tomedia($child['thumb']);}" width='30' height="30" onerror="$(this).remove()" style='padding:1px;border: 1px solid #ccc;float:left;' /> &nbsp;
                                [ID: {$child['id']}] {$child['name']}
                                <span class="pull-right">
                                    <div class='label {if $child['enabled']==1}label-success{else}label-default{/if}'
                                         {ifp 'goods.category.edit'}
                                         data-toggle='ajaxSwitch'
                                         data-switch-value='{$child['enabled']}'
                                         data-switch-value0='0|隐藏|label label-default|{php echo webUrl('goods/category/enabled',array('enabled'=>1,'id'=>$child['id']))}'
                                         data-switch-value1='1|显示|label label-success|{php echo webUrl('goods/category/enabled',array('enabled'=>0,'id'=>$child['id']))}'
                                         {/if}
                                         >
                                         {if $child['enabled']==1}显示{else}隐藏{/if}</div>

                                           {if intval($_W['shopset']['category']['level'])>2}
                                           {ifp 'goods.category.add'}<a class='btn btn-default btn-sm' href="{php echo webUrl('goods/category/add', array('parentid' => $child['id']))}" title='添加子分类' ><i class="fa fa-plus"></i></a>{/if}
                                           {/if}
                                           {ifp 'goods.category.edit|goods.category.view'}<a class='btn btn-default btn-sm' href="{php echo webUrl('goods/category/edit', array('id' => $child['id']))}" title="{ifp 'goods.category.edit'}修改{else}查看{/if}" ><i class="fa fa-edit"></i></a>{/if}
                                           {ifp 'goods.category.delete'} <a class='btn btn-default btn-sm'  data-toggle='ajaxPost'  href="{php echo webUrl('goods/category/delete', array('id' => $child['id']))}" data-confirm="确认删除此分类吗？"><i class="fa fa-remove"></i></a>{/if}
                                    </span>
                                </div>
                                {if count($children[$child['id']])>0 && intval($_W['shopset']['category']['level'])==3}

                                <ol class="dd-list"  style='width:100%;'>
                                    {loop $children[$child['id']] $third}
                                    <li class="dd-item" data-id="{$third['id']}">
                                        <div class="dd-handle">
                                            <img src="{php echo tomedia($third['thumb']);}" width='30' height="30" onerror="$(this).remove()" style='padding:1px;border: 1px solid #ccc;float:left;' /> &nbsp;
                                            [ID: {$third['id']}] {$third['name']}
                                            <span class="pull-right">
						 <div class='label {if $third['enabled']==1}label-success{else}label-default{/if}'
											{ifp 'goods.category.edit'}
											data-toggle='ajaxSwitch'
											data-switch-value='{$third['enabled']}'
											data-switch-value0='0|隐藏|label label-default|{php echo webUrl('goods/category/enabled',array('enabled'=>1,'id'=>$third['id']))}'
											data-switch-value1='1|显示|label label-success|{php echo webUrl('goods/category/enabled',array('enabled'=>0,'id'=>$third['id']))}'
											{/if}
											>
											{if $third['enabled']==1}显示{else}隐藏{/if}</div>
												
                                                {ifp 'goods.category.edit|goods.category.view'}<a class='btn btn-default btn-sm' href="{php echo webUrl('goods/category/edit', array('id' => $third['id']))}" title="{ifp 'goods.category.edit'}修改{else}查看{/if}" ><i class="fa fa-edit"></i></a>{/if}
                                                {ifp 'goods.category.delete'}<a class='btn btn-default btn-sm'  data-toggle='ajaxPost'  href="{php echo webUrl('goods/category/delete', array('id' => $third['id']))}" data-confirm="确认删除此分类吗？"><i class="fa fa-remove"></i></a>{/if}
                                            </span>
                                        </div>
                                    </li>
                                    {/loop}
                                </ol>
                                {/if}
                            </li>
                            {/loop}
                        </ol>
                        {/if}

                    </li>
                    {/if}
                    {/loop}

        </ol>
        <table class='table'>
            <tr>
                <td>

                    {ifp 'goods.category.edit'}
                    <input id="save_category" type="submit" class="btn btn-primary" value="保存">
                    {/if}
                    <input type="hidden" name="token" value="{$_W['token']}" />
                    <input type="hidden" name="datas" value="" />
                </td>
            </tr>
            </tbody>
        </table>
    </div>


</form>

        <script language='javascript'>
            myrequire(['jquery.nestable'], function () {

                $('#btnExpand').click(function () {
                    var action = $(this).data('action');
                    if (action === 'expand') {
                        $('#div_nestable').nestable('collapseAll');
                        $(this).data('action', 'collapse').html('<i class="fa fa-angle-up"></i> 展开所有');

                    } else {
                        $('#div_nestable').nestable('expandAll');
                        $(this).data('action', 'expand').html('<i class="fa fa-angle-down"></i> 折叠所有');
                    }
                })
                var depth = {php echo intval($_W['shopset']['category']['level'])};
                if (depth <= 0) {
                    depth = 2;
                }
                $('#div_nestable').nestable({maxDepth: depth});

                $('.dd-item').addClass('full');

                $(".dd-handle a,.dd-handle div").mousedown(function (e) {

                    e.stopPropagation();
                });
                var $expand = false;
                $('#nestableMenu').on('click', function (e)
                {
                    if ($expand) {
                        $expand = false;
                        $('.dd').nestable('expandAll');
                    } else {
                        $expand = true;
                        $('.dd').nestable('collapseAll');
                    }
                });

                $('form').submit(function(){
                    var json = window.JSON.stringify($('#div_nestable').nestable("serialize"));
                    $(':input[name=datas]').val(json);
                });

            })
        </script>

        {template '_footer'}

